package cn.home.web.controller.system;

import java.util.List;
import java.util.Set;

import cn.home.resident.domain.HomeUser;
import cn.home.resident.mapper.HomeUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.home.common.constant.Constants;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.core.domain.entity.SysMenu;
import cn.home.common.core.domain.entity.SysUser;
import cn.home.common.core.domain.model.LoginBody;
import cn.home.common.core.domain.model.LoginUser;
import cn.home.common.utils.SecurityUtils;
import cn.home.framework.web.service.SysLoginService;
import cn.home.framework.web.service.SysPermissionService;
import cn.home.framework.web.service.TokenService;
import cn.home.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    // 这里采用的是根据用户名和密码进行登录效果

    @GetMapping("/loginPhone")
    public AjaxResult loginPhone(String phone,String password)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginPhone( phone, password);
        ajax.put(Constants.TOKEN, token);
        // 登录成功后获取登录用户详细信息
        return ajax;
    }
    // 引入操作操作微信用户的userService对象
    @Autowired
    private HomeUserMapper userService;
    @GetMapping("getPhoneInfo")
    public AjaxResult getPhoneInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        // 根据userId查询微信登录的用户信息并返回前端【该方法需要自己定义哦】
        HomeUser wxuser = userService.selecthomeuserbysysuserid(user.getUserId().intValue());
        ajax.put("wxuser", wxuser);
        // 方便把当前登录的微信用户信息交给微信小程序进行保存操作
        return ajax;
    }
    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
